package com.study.example.simpleclient.panel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class FiveChessGame extends JFrame {
    // 游戏常量定义
    private static final int BOARD_SIZE = 15; // 15x15的棋盘
    private static final int CELL_SIZE = 40;  // 每个格子的大小
    private static final int MARGIN = 30;     // 棋盘边距

    // 游戏状态变量
    private int[][] board = new int[BOARD_SIZE][BOARD_SIZE]; // 0:空, 1:黑棋, 2:白棋
    private boolean isBlackTurn = true;  // 黑棋先行
    private boolean gameOver = false;    // 游戏是否结束
    private JPanel boardPanel;

    // 构造函数，初始化游戏
    public FiveChessGame() {
        initUI();
    }

    // 初始化用户界面
    private void initUI() {
        setTitle("五子棋游戏");
        setSize(BOARD_SIZE * CELL_SIZE + MARGIN * 2,
                BOARD_SIZE * CELL_SIZE + MARGIN * 3);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null); // 窗口居中
        setResizable(false);         // 窗口不可调整大小

        // 创建棋盘面板
        boardPanel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                drawBoard(g);   // 绘制棋盘
                drawPieces(g);  // 绘制棋子
            }
        };
        boardPanel.setBackground(new Color(240, 190, 100)); // 棋盘背景色(木色)
        add(boardPanel, BorderLayout.CENTER);
        addListeners();
    }

    // 添加游戏事件监听器
    private void addListeners() {
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (gameOver) return; // 游戏结束则不响应

                // 计算落子位置
                int x = e.getX();
                int y = e.getY();

                // 转换为棋盘坐标
                int row = (y - MARGIN * 2) / CELL_SIZE;
                int col = (x - MARGIN) / CELL_SIZE;

                // 检查位置是否有效
                if (row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE
                        && board[row][col] == 0) {
                    // 落子
                    board[row][col] = isBlackTurn ? 1 : 2;
                    System.out.println("下子 row:" + row + " col:" + col + " x=" + x + " y=" + y);
                    repaint(); // 重绘棋盘

                    // 检查是否获胜
                    if (checkWin(row, col)) {
                        String winner = isBlackTurn ? "黑棋" : "白棋";
                        JOptionPane.showMessageDialog(FiveChessGame.this, winner + "获胜！");
                        gameOver = true;
                    } else {
                        // 切换回合
                        isBlackTurn = !isBlackTurn;
                    }

                }
            }
        });
    }

    // 绘制棋盘
    private void drawBoard(Graphics g) {
        // 绘制网格线
        g.setColor(Color.BLACK);
        for (int i = 0; i <= BOARD_SIZE; i++) {
            // 横线
            g.drawLine(MARGIN, MARGIN + i * CELL_SIZE,
                    MARGIN + (BOARD_SIZE) * CELL_SIZE, MARGIN + i * CELL_SIZE);
            // 竖线
            g.drawLine(MARGIN + i * CELL_SIZE, MARGIN,
                    MARGIN + i * CELL_SIZE, MARGIN + (BOARD_SIZE) * CELL_SIZE);
        }

    }

    // 绘制棋子
    private void drawPieces(Graphics g) {
        for (int i = 0; i < BOARD_SIZE; i++) {
            for (int j = 0; j < BOARD_SIZE; j++) {
                if (board[i][j] != 0) {
                    int x = MARGIN + j * CELL_SIZE;
                    int y = MARGIN + i * CELL_SIZE;

                    // 绘制棋子
                    if (board[i][j] == 1) {
                        g.setColor(Color.BLACK);
                    } else {
                        g.setColor(Color.WHITE);
                    }
                    g.fillOval(x + 1,
                            y + 1,
                            CELL_SIZE - 2, CELL_SIZE - 2);

                    // 绘制棋子边框
                    g.setColor(Color.BLACK);
                    g.drawOval(x + 1,
                            y + 1,
                            CELL_SIZE - 2, CELL_SIZE - 2);
                }
            }
        }
    }

    // 检查是否获胜
    private boolean checkWin(int row, int col) {
        int color = board[row][col];

        // 检查水平方向
        if (checkDirection(row, col, 0, 1, color) >= 5) return true;

        // 检查垂直方向
        if (checkDirection(row, col, 1, 0, color) >= 5) return true;

        // 检查对角线 \
        if (checkDirection(row, col, 1, 1, color) >= 5) return true;

        // 检查对角线 /
        if (checkDirection(row, col, 1, -1, color) >= 5) return true;

        return false;
    }

    // 检查某个方向上的连续棋子数量
    private int checkDirection(int row, int col, int rowDir, int colDir, int color) {
        int count = 1; // 当前位置已有一个棋子

        // 正向检查
        for (int i = 1; ; i++) {
            int newRow = row + i * rowDir;
            int newCol = col + i * colDir;

            if (newRow < 0 || newRow >= BOARD_SIZE || newCol < 0 || newCol >= BOARD_SIZE
                    || board[newRow][newCol] != color) {
                break;
            }
            count++;
        }

        // 反向检查
        for (int i = 1; ; i++) {
            int newRow = row - i * rowDir;
            int newCol = col - i * colDir;

            if (newRow < 0 || newRow >= BOARD_SIZE || newCol < 0 || newCol >= BOARD_SIZE
                    || board[newRow][newCol] != color) {
                break;
            }
            count++;
        }

        return count;
    }

    // 重新开始游戏
    private void restartGame() {
        board = new int[BOARD_SIZE][BOARD_SIZE];
        isBlackTurn = true;
        gameOver = false;
        repaint();
    }

    // 主方法，启动游戏
    public static void main(String[] args) {
        // 在事件调度线程中运行Swing应用
        SwingUtilities.invokeLater(() -> {
            FiveChessGame game = new FiveChessGame();
            game.setVisible(true);
            game.requestFocus();
        });
    }
}

